import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2025-07-15
 * Time: 22:14
 */
public class Test52 {
    static int[] dx = {1,-1,0,0};
    static int[] dy = {0,0,1,-1};
    static int m;
    static int n;
    static boolean[][] vis;
    public static int numIslands(char[][] grid) {
        m = grid.length;
        n = grid[0].length;
        int ret =  0;
        vis = new boolean[m][n];
        for(int i = 0; i < m; i++) {
            for(int j = 0; j < n; j++) {
                if(grid[i][j] == '1' && !vis[i][j]) {
                    ret++;
                    bfs(grid,i,j);
                }
            }
        }
        return ret;
    }

    static void bfs(char[][] grid,int i,int j) {
        vis[i][j] = true;
        for(int k = 0; k < 4; k++) {
            int x = i+dx[k],y = j+dy[k];
            if(x >= 0 && x < m && y >= 0 && y < n && grid[x][y] == '1' && !vis[x][y]) {
                bfs(grid,x,y);
            }
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n1 = sc.nextInt();
        int n2 = sc.nextInt();
        char[][] num = new char[n1][n2];
        for(int i = 0; i < n1; i++) {
            for(int j = 0; j < n2; j++) {
                num[i][j] = sc.next().charAt(0);
            }
        }
        int ret = numIslands(num);
        System.out.println(ret);
    }
}
